<?php

namespace app\home\controller;

use think\Db;
use think\Request;
use think\Config;
use clt\Form;

class EmptyController extends Common{

    protected  $dao,$fields;

    function __construct(Request $request = null)
    {        
        parent::__construct($request);
        
        $this->template_list = 'template_list';
        $this->template_show = 'template_show';
    }

    public function index(){
        //单页模型
        if(DBNAME=='page'){
            $info = $this->dao->where(['id'=>input('catId'),'status'=>1])->find();
            $this->assign('info',$info);
            // print_r($info);
            if($info['template']){
                $template = $info['template'];
            }else{
                $info['template'] = $this->category->where(['id'=>$info['id']])->value($this->template_show);
                if($info['template']){
                    $template = $info['template'];
                }else{
                    $template = DBNAME.'_show';
                }
            }
            return $this->fetch($template);
        }else{
            //图片模型
            if(DBNAME=='picture'){
                $setup = $this->db->name('field')->where(array('moduleid'=>3,'field'=>'group'))->value('setup');
                $setup=is_array($setup) ? $setup: string2array($setup);
                $options = explode("\n",$setup['options']);
                foreach($options as $r) {
                    $v = explode("|",$r);
                    $k = trim($v[1]);
                    $optionsarr[$k]['val'] = $v[0];
                    $optionsarr[$k]['key'] = $k;
                }
                $this->assign('options',$optionsarr);
            }else{
                //其他列表模型
                $arrchildid = $this->db->name('category')->where(['id'=>input('catId')])->value('arrchildid');
                $map = ['status'=>1];//审核状态
                if($arrchildid!=input('catId')){
                    $map['catId'] = ['in',$arrchildid];
                    $map1['catId'] = ['in',$arrchildid];
                }else{
                    $map['catId'] = input("catId");
                    $map1['catId'] = input("catId");
                }
                $map['aid'] = $this->new_aid;//地区id

                // halt($map);
                $map1 = $this->map1;
                $list=$this->db->name('article')->alias('a')
                    ->join(config('database.prefix').'category c','a.catid = c.id','left')
                    ->where($map)
                    // ->whereOr($map1)
                    ->whereOr(function ($q) use($map1) {
                        $q->where($map1);
                    })
                    // ->fetchSql(true)
                    ->field('a.*,c.catdir')
                    ->order('sort asc,updatetime desc')
                    ->paginate($this->pagesize);
                // halt($this->db);
                // halt($list);
                
                // 获取分页显示
                $page = $list->render();
                $list = $list->toArray();
                foreach ($list['data'] as $k=>$v){
                    $list['data'][$k]['controller'] = $v['catdir'];
                    if(isset($v['thumb'])){
                        $list['data'][$k]['title_thumb'] =imgUrl($v['thumb'],'/static/home/img/list'.($k+1).'.jpg');
                    }else{
                        $list['data'][$k]['title_thumb'] ='/static/home/img/list'.($k+1).'.jpg';
                    }
                }
                //halt($list['data']);
                //print_r($page);
                $this->assign('list',$list['data']);
                $this->assign('page',$page);
            }
            $cattemplate = $this->category->where('id',input('catId'))->value($this->template_list);
            $template =$cattemplate ? $cattemplate : DBNAME.'_list';
            // halt($this->template_list);
            
            //服务站点
            if($this->controller == 'Fwzd'){
                if($this->action == 'videos'){
                    $this->videos();//视频监控
                }else{
                    $this->fwzd();                    
                }
            }
            return $this->fetch($template);
        }
    }

    public function info(){
        $this->dao->where('id',input('id'))->setInc('hits');
        $info = $this->dao->where('id',input('id'))->find();
        $info['pic'] =  isset($info['pic'])?$info['pic']:"/static/home/img/list".rand(1,3).".jpg";
        // $title_style = explode(';',$info['title_style']);
        // $info['title_color'] = $title_style[0];
        // $info['title_weight'] = $title_style[1];
        $title_thumb = $info['thumb'];
        $info['title_thumb'] = $title_thumb?$title_thumb:'/static/home/img/list'.rand(1,3).'.jpg';
        if(DBNAME=='picture'){
            $pics = explode(':::',$info['pics']);
            foreach ($pics as $k=>$v){
                $info['pics'][$k] = explode('|',$v);
            }
        }
        //详情内容翻页
        $id = input('id');
        $cid = input('catId');
        //上一篇
        $map['id'] = array('lt',$id);
        $map['aid'] = $this->new_aid;
        $prev = $this->dao->where(['status' => 1,'catid' => $cid]) ->where($map)->order('id desc')->find();
        //下一篇
        $map['id'] = array('gt',$id);
        $map['aid'] = $this->new_aid;
        $next = $this->dao->where(['status' => 1,'catid' => $cid]) ->where($map)->order('id asc')->find();
        $this->assign('Prev',$prev);
        $this->assign('Next',$next);
        $this->assign('info',$info);

        if($info['template']){
            $template = $info['template'];
        }else{
            $cattemplate = $this->category->where('id',$info['catid'])->value($this->template_show);
            if($cattemplate){
                $template = $cattemplate;
            }else{
                $template = DBNAME.'_show';
            }
        }
        return $this->fetch($template);
    }

    protected function fetch($template = '', $vars = [], $replace = [], $config = [], $pathhandl = true)
    {
        if($template == '') {
            // 如果传入的模板为空则调用当前action方法
            $template = strtolower($this->request->action());
        }
        $this->template_dir = "view/".$template;
        $config['view_path'] = Config::get("template.view_path");
        //halt($this->template_dir);
        return parent::fetch($this->template_dir, $vars, $replace, $config, false); // TODO: Change the autogenerated stub
    }
    
    //服务站点
    public function fwzd(){
        $dom = $this->domainInfo;
        $did = input('did');
        if($did){
            $dom = get_domain('id', $did);
        }
        // $subIitem = $this->area->getLists(['area_parent_id' => $dom['zone_id']], 'area_sort asc', 'area_id,area_name,area_parent_id');
        $dom['sub'] = $this->getDomainTree($dom['zone_id']);
        $this->assign('dom', $dom);
        // halt($dom);
    }
    
    //视频监控
    public function videos(){
        $dom = $this->domainInfo;
        $d = input('d');
        if($d){
            $dom = get_domain('id', $d);
        }
        // $subIitem = $this->area->getLists(['area_parent_id' => $dom['zone_id']], 'area_sort asc', 'area_id,area_name,area_parent_id');
        $dom['sub'] = $this->getDomainTree($dom['zone_id']);
        $this->assign('dom', $dom);
        
        $site_streams = Db::name("VsStream")->where(['site_id' => $d, 'status' => 1])->order('sort desc')->column('id,id,title,desc,created_at');
        $this->assign('site_streams', $site_streams);
        // halt($site_streams);
        return $this->fetch('');
    }

    //服务站点详情
    public function fwzdInfo(){
        $this->fwzd();
        return $this->fetch('article_show_fwzd');
    }
}
